/**
 * 
 */
package util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.http.Cookie;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import bean.CookieInfoBean;

/**
 * @author ben
 *
 */
public class CookieUtil {

	private static final Log log = LogFactory.getLog(CookieUtil.class);

	//cookie name
	private static final String COOKIE_NAME = "userinfo";

	//cookie field key : userid
	private static final String COOKIE_FIELD_USER_ID = "userid";

	//cookie field password : password
	private static final String COOKIE_FIELD_PASSWORD = "password";

	public static Cookie GenerateCookie(String userId, String password, int expiry) {
		Cookie ticket = null;
		try {
			String formattedPassword = "/XXXXX," + password + ",XXXXX";
			ticket = new Cookie(
					COOKIE_NAME,
					COOKIE_FIELD_USER_ID
							+ "="
							+ userId
							+ "&"
							+ COOKIE_FIELD_PASSWORD
							+ "="
							+ URLEncoder.encode(formattedPassword, "UTF-8")
							+ "&sign=%5Cb%EF%21%80%08%FC%1E%98%07%D2vb%9D%A5m%21%1AW%1C%A5%F4%B1%1E%E4%89%3F%8F%97%1C%B7%C8%23O7%40%8Fg%FC%DB%014%96%C9%B1%CF9%AF%C7%5B%2EJ37%E0%86%CF%F2%EBC%9A%DB%F6%26%3A%DD%2D%C2%F9%11%2F%22J%FCd%7C%8E%23%90%2AP%C0b%0BD%E5%09%A4%BE%C5%C1W%02y%1A%D2u%7DB%1E%DE%93%3D%22%2E%A05x%9E%29%3D%A0%A7H%93%93%60%16%7CsL%F4%85h%0E%E5%5E%E7");
			ticket.setPath("/");

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return ticket;
	}
	
	public static Cookie GenerateEmptyCookie() {
		Cookie ticket = new Cookie(COOKIE_NAME, null);
		ticket.setMaxAge(0);
		ticket.setPath("/");
		return ticket;
	}

	/**
	 * cookieの情報を、beanに変換します
	 * @param cookies クライアントから送信されたcookie情報
	 * @return CookieInfoBean
	 * @throws UnsupportedEncodingException 
	 */
	public static CookieInfoBean createCookieInfo(Cookie[] cookies)
			throws UnsupportedEncodingException {

		CookieInfoBean bean = new CookieInfoBean();

		//get the cookie info which is necessary
		for (Cookie cookie : cookies) {

			String cookieName = cookie.getName();
			String cookieValue = cookie.getValue();

			if (log.isDebugEnabled()) {
				log.debug("[cookie]name:" + cookieName + " value:"
						+ cookieValue);
			}

			if (cookieName.equals(COOKIE_NAME)) {

				String[] ticketElems = StringUtils.split(cookieValue, "&");

				for (String elem : ticketElems) {
					String[] splitElem = StringUtils.split(elem, "=");

					if (splitElem.length == 2) {
						String elemName = splitElem[0];
						String elemValue = splitElem[1];

						if (elemName.equals(COOKIE_FIELD_USER_ID)) {
							bean.setUserId(elemValue);
						} else if (elemName.equals(COOKIE_FIELD_PASSWORD)) {
							String[] passwords = StringUtils.split(URLDecoder
									.decode(elemValue, "UTF-8"), ",");
							bean.setPassword(passwords[1]);
						}
					} else {
						//if the style is not "xxx=yyy"
						log.debug("bad style for cookie:" + elem);
					}
				}

			}

		}

		if (log.isDebugEnabled()) {
			log.debug("[cookie]CookieInfoBean:"
					+ ToStringBuilder.reflectionToString(bean,
							ToStringStyle.MULTI_LINE_STYLE));
		}

		return bean;

	}
}
